package com.hssoftvn.android.apps.tinycouplechat;

import java.io.File;

import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Looper;
import android.widget.Toast;

public class Utils {
	
	public static String ApplicationDataPath =  Environment.getExternalStorageDirectory().getPath() + "android/data/com.hssoftvn.android.apps.tinycouplechat/files";	
	public static String AvatarPath =  ApplicationDataPath + "/avatars";
	
	public final static int TYPE_ME = 0; //Sender
	public final static int TYPE_YOU = 1; //Receiver
	
	public final static int SEX_FEMALE = 0; //Male
	public final static int SEX_MALE = 1; //Female
	
	public static int STATUS_UNKNOWN = -1;
	public static int STATUS_SENDING = 0;
	public static int STATUS_SENT = 1;
	public static int STATUS_DELEVERED = 2;
	public static int STATUS_SEEN = 3;
	
	public static String INTENT_MSG_RECEIVED = "com.hssoftvn.android.ACTION_MSG_RECEIVED";
	public static String EXTRA_INCOMING_MSG = "incommingmsg";
	
	public static TinyCoupleChat getApplication(){
		return TinyCoupleChat.getInstance();
	}
	private static Toast mToast = null;
	public static void ShowToast(String text)
	{
		final String _text = text;
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try{
					Looper.prepare();
					if (mToast==null)
						mToast = Toast.makeText(getApplication(), "", Toast.LENGTH_SHORT);
					mToast.cancel();
					mToast.setDuration(Toast.LENGTH_SHORT);
					mToast.setText(_text);
					mToast.show();
					Looper.loop();
				}catch(Exception eee){
				}				
			}
		}).start();		
	}
	
	public static Drawable getAvatarByName(String name){
	
		Drawable d = null;			
		
		name = ApplicationDataPath + "/" + name +".jpg";	
				
		try {
			d = Drawable.createFromPath(name);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			ShowToast("No priavte avatar!\nDefault avatar will be used.");			
		}
		
		return d;
	}
	
	public static Drawable getDefaultAvatarBySex(boolean isMale){
		
		Drawable d = null;
		
		if (isMale)
			d = getApplication().getResources().getDrawable(R.drawable.male_avatar);
		else
			d = getApplication().getResources().getDrawable(R.drawable.female_avatar);			
		
		return d;
	}		
	
	public static Drawable getAvatarByUser(ChatUser user){
		Drawable d=null;
		
		d = getAvatarByName(user.getUsername());
		if (d==null)
			d = getDefaultAvatarBySex(user.IsMale());
		
		return d;
	}
	
	public static String getSafeRequestString(String string)
	{
		String safe = string;
		
		// \ -> \\
		//\b  \t  \n  \f  \r  \"  \'  \\
		/*
		safe = string.replace("\\", "\\\\");
		safe = string.replace("\\b", "\\\\n");
		safe = string.replace("\\t", "\\\\r");
		safe = string.replace("\\n", "\\\\b");
		safe = string.replace("\\f", "\\\\");
		safe = string.replace("\\r", "\\\\");
		safe = string.replace("\"", "\\\"");
		*/
		safe = string.replace("|", "_|_");

		return safe;
	}

	public static String getSafeResponseString(String string)
	{
		String safe = string;
		
		/* \ -> \\
		//\b  \t  \n  \f  \r  \"  \'  \\
		safe = string.replace("\\\\", "\\");
		safe = string.replace("\\\\n", "\\b");
		safe = string.replace( "\\\\r", "\\t");
		safe = string.replace( "\\\\b", "\\n");
		safe = string.replace( "\\\\", "\\f");
		safe = string.replace( "\\\\", "\\r");
		safe = string.replace( "\\\"", "\"");
		*/
		safe = string.replace("_|_", "|");

		return safe;
	}
	
}
